package com.task.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.task.bean.Rank;
import com.task.bean.Teacher;
import com.task.service.ITeacherService;
import com.task.vo.ResultVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liuzhe
 * @since 2021-04-10
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @PostMapping("/add")
    public ResultVo add(@RequestBody Teacher teacher) {
        return ResultVo.success(teacherService.save(teacher));
    }

    @PostMapping("/list")
    public ResultVo list(int pageNum, int pageSize) {
        IPage page = new Page(pageNum, pageSize);
        return ResultVo.success(teacherService.page(page).getRecords());
    }

    @PostMapping("/login")
    public ResultVo login(@RequestBody Teacher teacher) {
        Teacher result = teacherService.getByUsernameAndPassword(teacher);
        if (ObjectUtils.isNotEmpty(result)) {
            return ResultVo.success(result);
        } else {
            return ResultVo.failed("登录失败");
        }
    }

    @GetMapping("/queryTea")
    public ResultVo queryStu(int pageNum, int pageSize) {
        return ResultVo.success(teacherService.queryTeaPage(pageNum, pageSize));
    }

    @DeleteMapping("/delete")
    public ResultVo delete(int id) {
        return ResultVo.success(teacherService.removeById(id));
    }


}

